import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { ViewService } from "./view.service";
import { TableComponent } from "../../modal/table/table.component";
import * as _ from 'lodash';

@Component({
  selector: 'app-view',
  templateUrl: './view.component.html',
  styleUrls: ['./view.component.scss'],
  providers: [ViewService]
})
export class ViewComponent implements OnInit {
  // 模版
  @ViewChild("viewsTable", { read: ViewContainerRef })
  viewsTable: ViewContainerRef;
  viewsTableComponent: ComponentRef<TableComponent>;
  // 用于识别指标类型
  public type: any;
  // 列表新数据
  public dataList: any = [];
  // 克隆列表新数据,用于页面数据标红对比
  public isChangeList: any = [];
  // 用于是否显示下拉框
  public isEditList: any = [];

  constructor(
    public resolver: ComponentFactoryResolver,
    public viewService: ViewService
  ) { }

  ngOnInit() {
    // 加载table
    this.showViewsTable();
  }
  // 模版
  showViewsTable() {
    let that = this;
    if (this.viewsTableComponent && this.viewsTableComponent.instance) {
      this.viewsTableComponent.destroy();
    }
    new Promise((resolve, reject) => {
      const viewsTableComponent = that.resolver.resolveComponentFactory(TableComponent);
      that.viewsTableComponent = that.viewsTable.createComponent(viewsTableComponent);
      that.viewsTableComponent.instance.isShowCol1 = 'sFundTypeZh';
      that.viewsTableComponent.instance.isShowCol2 = 'viewsFullName';
      that.viewsTableComponent.instance.isShowCol3 = 'sStatus';
      // 用于标红对比
      that.isChangeList = _.cloneDeep(that.dataList);
      // 用于是否显示下拉框
      that.isEditList = _.cloneDeep(that.dataList);
      _.forEach(that.isChangeList, item => {
        // 将所有赋false,查看页面不需要标红
        _.forIn(item, (value, key) => {
          item[key] = false;
        })
      })
      _.forEach(that.isEditList, item => {
        // 将所有赋'no',查看页面不需要显示下拉框
        _.forIn(item, (value, key) => {
          item[key] = 'no';
        })
      })
      resolve(resolve);
    }).then(resolve => {
      that.viewsTableComponent.instance.dataList = _.cloneDeep(that.dataList);
      that.viewsTableComponent.instance.isChangeList = _.cloneDeep(that.isChangeList);
      that.viewsTableComponent.instance.isEditList = _.cloneDeep(that.isEditList);
    })
  }
}
